GeoPandas

import geopandas
/tmp/ipykernel_3120570/1616829109.py:1: DeprecationWarning: Shapely 2.0 is installed, but because PyGEOS is also installed, GeoPandas still uses PyGEOS by default. However, starting with version 0.14, the default will switch to Shapely. To force to use Shapely 2.0 now, you can either uninstall PyGEOS or set the environment variable USE_PYGEOS=0. You can do this before starting the Python process, or in your code before importing geopandas:

import os
os.environ['USE_PYGEOS'] = '0'
import geopandas

In the next release, GeoPandas will switch to using Shapely by default, even if PyGEOS is installed. If you only have PyGEOS installed to get speed-ups, this switch should be smooth. However, if you are using PyGEOS directly (calling PyGEOS functions on geometries from GeoPandas), this will then stop working and you are encouraged to migrate from PyGEOS to Shapely 2.0 (https://shapely.readthedocs.io/en/latest/migration_pygeos.html).
  import geopandas
gdf = geopandas.read_file("./data/shared/covid/gz_2010_us_040_00_500k.json")
ERROR 1: PROJ: proj_create_from_database: Open of /opt/tljh/user/share/proj failed
gdf.head()
GEO_ID STATE NAME LSAD CENSUSAREA geometry
0 0400000US23 23 Maine 30842.923 MULTIPOLYGON (((-67.61976 44.51975, -67.61541 ...
1 0400000US25 25 Massachusetts 7800.058 MULTIPOLYGON (((-70.83204 41.60650, -70.82373 ...
2 0400000US26 26 Michigan 56538.901 MULTIPOLYGON (((-88.68443 48.11579, -88.67563 ...
3 0400000US30 30 Montana 145545.801 POLYGON ((-104.05770 44.99743, -104.25015 44.9...
4 0400000US32 32 Nevada 109781.180 POLYGON ((-114.05060 37.00040, -114.04999 36.9...
gdf.columns.values
array(['GEO_ID', 'STATE', 'NAME', 'LSAD', 'CENSUSAREA', 'geometry'],
      dtype=object)
gdf.plot()
<AxesSubplot:>

type(gdf)
geopandas.geodataframe.GeoDataFrame

Geometry

gdf.geometry
0     MULTIPOLYGON (((-67.61976 44.51975, -67.61541 ...
1     MULTIPOLYGON (((-70.83204 41.60650, -70.82373 ...
2     MULTIPOLYGON (((-88.68443 48.11579, -88.67563 ...
3     POLYGON ((-104.05770 44.99743, -104.25015 44.9...
4     POLYGON ((-114.05060 37.00040, -114.04999 36.9...
5     POLYGON ((-75.52684 39.65571, -75.52634 39.656...
6     MULTIPOLYGON (((-71.94356 41.28668, -71.92680 ...
7     MULTIPOLYGON (((-82.60288 36.03983, -82.60074 ...
8     MULTIPOLYGON (((-82.81349 41.72347, -82.81049 ...
9     POLYGON ((-75.41504 39.80179, -75.42804 39.809...
10    MULTIPOLYGON (((-71.28157 41.64821, -71.27817 ...
11    POLYGON ((-81.67754 36.58812, -81.68014 36.585...
12    MULTIPOLYGON (((-97.13436 27.89633, -97.13360 ...
13    POLYGON ((-114.05060 37.00040, -114.05175 37.0...
14    MULTIPOLYGON (((-123.09055 49.00198, -123.0353...
15    MULTIPOLYGON (((-90.45525 47.02400, -90.45713 ...
16    MULTIPOLYGON (((-65.58733 18.38199, -65.59122 ...
17    MULTIPOLYGON (((-76.07147 38.20350, -76.04879 ...
18    MULTIPOLYGON (((-85.00237 31.00068, -85.02411 ...
19    MULTIPOLYGON (((-164.97620 54.13459, -164.9377...
20    POLYGON ((-109.04522 36.99908, -109.04524 36.9...
21    POLYGON ((-94.55929 36.49950, -94.51948 36.499...
22    MULTIPOLYGON (((-122.44632 37.86105, -122.4385...
23    POLYGON ((-102.04224 36.99308, -102.05450 36.9...
24    MULTIPOLYGON (((-71.85957 41.32240, -71.86823 ...
25    MULTIPOLYGON (((-75.55945 39.62981, -75.55910 ...
26    POLYGON ((-77.03860 38.79151, -77.03890 38.800...
27    MULTIPOLYGON (((-85.15641 29.67963, -85.13740 ...
28    POLYGON ((-81.44412 30.70971, -81.44872 30.709...
29    MULTIPOLYGON (((-171.73761 25.79210, -171.7223...
30    POLYGON ((-111.04669 42.00157, -111.41587 42.0...
31    POLYGON ((-87.53233 39.99778, -87.53254 39.987...
32    POLYGON ((-88.02803 37.79922, -88.02938 37.803...
33    POLYGON ((-95.76565 40.58521, -95.75889 40.588...
34    POLYGON ((-94.61808 36.99813, -94.62522 36.998...
35    MULTIPOLYGON (((-83.67541 36.60081, -83.67561 ...
36    MULTIPOLYGON (((-88.86507 29.75271, -88.88975 ...
37    POLYGON ((-91.37161 43.50095, -91.37695 43.500...
38    MULTIPOLYGON (((-88.71072 30.25080, -88.65680 ...
39    POLYGON ((-89.53910 36.49820, -89.53452 36.491...
40    POLYGON ((-95.76565 40.58521, -95.76853 40.583...
41    MULTIPOLYGON (((-72.45852 42.72685, -72.45849 ...
42    POLYGON ((-109.05004 31.33250, -109.05017 31.4...
43    POLYGON ((-96.56328 45.93524, -96.57690 45.935...
44    POLYGON ((-94.61792 36.49941, -94.61531 36.484...
45    POLYGON ((-117.22007 44.30138, -117.22245 44.2...
46    POLYGON ((-78.54109 33.85111, -78.55394 33.847...
47    POLYGON ((-96.44341 42.48949, -96.45971 42.486...
48    POLYGON ((-72.04008 44.15575, -72.04271 44.152...
49    MULTIPOLYGON (((-76.04653 37.95359, -76.04169 ...
50    POLYGON ((-81.96830 37.53780, -81.96540 37.541...
51    POLYGON ((-109.05008 41.00066, -109.17368 41.0...
Name: geometry, dtype: geometry
gdf.iloc[0].geometry

gdf.shape
(52, 6)
gdf.head(52)
GEO_ID STATE NAME LSAD CENSUSAREA geometry
0 0400000US23 23 Maine 30842.923 MULTIPOLYGON (((-67.61976 44.51975, -67.61541 ...
1 0400000US25 25 Massachusetts 7800.058 MULTIPOLYGON (((-70.83204 41.60650, -70.82373 ...
2 0400000US26 26 Michigan 56538.901 MULTIPOLYGON (((-88.68443 48.11579, -88.67563 ...
3 0400000US30 30 Montana 145545.801 POLYGON ((-104.05770 44.99743, -104.25015 44.9...
4 0400000US32 32 Nevada 109781.180 POLYGON ((-114.05060 37.00040, -114.04999 36.9...
5 0400000US34 34 New Jersey 7354.220 POLYGON ((-75.52684 39.65571, -75.52634 39.656...
6 0400000US36 36 New York 47126.399 MULTIPOLYGON (((-71.94356 41.28668, -71.92680 ...
7 0400000US37 37 North Carolina 48617.905 MULTIPOLYGON (((-82.60288 36.03983, -82.60074 ...
8 0400000US39 39 Ohio 40860.694 MULTIPOLYGON (((-82.81349 41.72347, -82.81049 ...
9 0400000US42 42 Pennsylvania 44742.703 POLYGON ((-75.41504 39.80179, -75.42804 39.809...
10 0400000US44 44 Rhode Island 1033.814 MULTIPOLYGON (((-71.28157 41.64821, -71.27817 ...
11 0400000US47 47 Tennessee 41234.896 POLYGON ((-81.67754 36.58812, -81.68014 36.585...
12 0400000US48 48 Texas 261231.711 MULTIPOLYGON (((-97.13436 27.89633, -97.13360 ...
13 0400000US49 49 Utah 82169.620 POLYGON ((-114.05060 37.00040, -114.05175 37.0...
14 0400000US53 53 Washington 66455.521 MULTIPOLYGON (((-123.09055 49.00198, -123.0353...
15 0400000US55 55 Wisconsin 54157.805 MULTIPOLYGON (((-90.45525 47.02400, -90.45713 ...
16 0400000US72 72 Puerto Rico 3423.775 MULTIPOLYGON (((-65.58733 18.38199, -65.59122 ...
17 0400000US24 24 Maryland 9707.241 MULTIPOLYGON (((-76.07147 38.20350, -76.04879 ...
18 0400000US01 01 Alabama 50645.326 MULTIPOLYGON (((-85.00237 31.00068, -85.02411 ...
19 0400000US02 02 Alaska 570640.950 MULTIPOLYGON (((-164.97620 54.13459, -164.9377...
20 0400000US04 04 Arizona 113594.084 POLYGON ((-109.04522 36.99908, -109.04524 36.9...
21 0400000US05 05 Arkansas 52035.477 POLYGON ((-94.55929 36.49950, -94.51948 36.499...
22 0400000US06 06 California 155779.220 MULTIPOLYGON (((-122.44632 37.86105, -122.4385...
23 0400000US08 08 Colorado 103641.888 POLYGON ((-102.04224 36.99308, -102.05450 36.9...
24 0400000US09 09 Connecticut 4842.355 MULTIPOLYGON (((-71.85957 41.32240, -71.86823 ...
25 0400000US10 10 Delaware 1948.543 MULTIPOLYGON (((-75.55945 39.62981, -75.55910 ...
26 0400000US11 11 District of Columbia 61.048 POLYGON ((-77.03860 38.79151, -77.03890 38.800...
27 0400000US12 12 Florida 53624.759 MULTIPOLYGON (((-85.15641 29.67963, -85.13740 ...
28 0400000US13 13 Georgia 57513.485 POLYGON ((-81.44412 30.70971, -81.44872 30.709...
29 0400000US15 15 Hawaii 6422.628 MULTIPOLYGON (((-171.73761 25.79210, -171.7223...
30 0400000US16 16 Idaho 82643.117 POLYGON ((-111.04669 42.00157, -111.41587 42.0...
31 0400000US17 17 Illinois 55518.930 POLYGON ((-87.53233 39.99778, -87.53254 39.987...
32 0400000US18 18 Indiana 35826.109 POLYGON ((-88.02803 37.79922, -88.02938 37.803...
33 0400000US19 19 Iowa 55857.130 POLYGON ((-95.76565 40.58521, -95.75889 40.588...
34 0400000US20 20 Kansas 81758.717 POLYGON ((-94.61808 36.99813, -94.62522 36.998...
35 0400000US21 21 Kentucky 39486.338 MULTIPOLYGON (((-83.67541 36.60081, -83.67561 ...
36 0400000US22 22 Louisiana 43203.905 MULTIPOLYGON (((-88.86507 29.75271, -88.88975 ...
37 0400000US27 27 Minnesota 79626.743 POLYGON ((-91.37161 43.50095, -91.37695 43.500...
38 0400000US28 28 Mississippi 46923.274 MULTIPOLYGON (((-88.71072 30.25080, -88.65680 ...
39 0400000US29 29 Missouri 68741.522 POLYGON ((-89.53910 36.49820, -89.53452 36.491...
40 0400000US31 31 Nebraska 76824.171 POLYGON ((-95.76565 40.58521, -95.76853 40.583...
41 0400000US33 33 New Hampshire 8952.651 MULTIPOLYGON (((-72.45852 42.72685, -72.45849 ...
42 0400000US35 35 New Mexico 121298.148 POLYGON ((-109.05004 31.33250, -109.05017 31.4...
43 0400000US38 38 North Dakota 69000.798 POLYGON ((-96.56328 45.93524, -96.57690 45.935...
44 0400000US40 40 Oklahoma 68594.921 POLYGON ((-94.61792 36.49941, -94.61531 36.484...
45 0400000US41 41 Oregon 95988.013 POLYGON ((-117.22007 44.30138, -117.22245 44.2...
46 0400000US45 45 South Carolina 30060.696 POLYGON ((-78.54109 33.85111, -78.55394 33.847...
47 0400000US46 46 South Dakota 75811.000 POLYGON ((-96.44341 42.48949, -96.45971 42.486...
48 0400000US50 50 Vermont 9216.657 POLYGON ((-72.04008 44.15575, -72.04271 44.152...
49 0400000US51 51 Virginia 39490.086 MULTIPOLYGON (((-76.04653 37.95359, -76.04169 ...
50 0400000US54 54 West Virginia 24038.210 POLYGON ((-81.96830 37.53780, -81.96540 37.541...
51 0400000US56 56 Wyoming 97093.141 POLYGON ((-109.05008 41.00066, -109.17368 41.0...
drop_states = ['15', '02', '72'] # HA, AK, PR
gdf[gdf.STATE.isin(drop_states)].plot()
<AxesSubplot:>

gdf[~gdf.STATE.isin(drop_states)].plot()
<AxesSubplot:>

gdf = gdf[~gdf.STATE.isin(drop_states)]
gdf.shape
(49, 6)
gdf.index
Index([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 17, 18,
       20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38,
       39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51],
      dtype='int64')
gdf.reset_index(inplace=True)
gdf.index
RangeIndex(start=0, stop=49, step=1)
gdf.head(49)
index GEO_ID STATE NAME LSAD CENSUSAREA geometry
0 0 0400000US23 23 Maine 30842.923 MULTIPOLYGON (((-67.61976 44.51975, -67.61541 ...
1 1 0400000US25 25 Massachusetts 7800.058 MULTIPOLYGON (((-70.83204 41.60650, -70.82373 ...
2 2 0400000US26 26 Michigan 56538.901 MULTIPOLYGON (((-88.68443 48.11579, -88.67563 ...
3 3 0400000US30 30 Montana 145545.801 POLYGON ((-104.05770 44.99743, -104.25015 44.9...
4 4 0400000US32 32 Nevada 109781.180 POLYGON ((-114.05060 37.00040, -114.04999 36.9...
5 5 0400000US34 34 New Jersey 7354.220 POLYGON ((-75.52684 39.65571, -75.52634 39.656...
6 6 0400000US36 36 New York 47126.399 MULTIPOLYGON (((-71.94356 41.28668, -71.92680 ...
7 7 0400000US37 37 North Carolina 48617.905 MULTIPOLYGON (((-82.60288 36.03983, -82.60074 ...
8 8 0400000US39 39 Ohio 40860.694 MULTIPOLYGON (((-82.81349 41.72347, -82.81049 ...
9 9 0400000US42 42 Pennsylvania 44742.703 POLYGON ((-75.41504 39.80179, -75.42804 39.809...
10 10 0400000US44 44 Rhode Island 1033.814 MULTIPOLYGON (((-71.28157 41.64821, -71.27817 ...
11 11 0400000US47 47 Tennessee 41234.896 POLYGON ((-81.67754 36.58812, -81.68014 36.585...
12 12 0400000US48 48 Texas 261231.711 MULTIPOLYGON (((-97.13436 27.89633, -97.13360 ...
13 13 0400000US49 49 Utah 82169.620 POLYGON ((-114.05060 37.00040, -114.05175 37.0...
14 14 0400000US53 53 Washington 66455.521 MULTIPOLYGON (((-123.09055 49.00198, -123.0353...
15 15 0400000US55 55 Wisconsin 54157.805 MULTIPOLYGON (((-90.45525 47.02400, -90.45713 ...
16 17 0400000US24 24 Maryland 9707.241 MULTIPOLYGON (((-76.07147 38.20350, -76.04879 ...
17 18 0400000US01 01 Alabama 50645.326 MULTIPOLYGON (((-85.00237 31.00068, -85.02411 ...
18 20 0400000US04 04 Arizona 113594.084 POLYGON ((-109.04522 36.99908, -109.04524 36.9...
19 21 0400000US05 05 Arkansas 52035.477 POLYGON ((-94.55929 36.49950, -94.51948 36.499...
20 22 0400000US06 06 California 155779.220 MULTIPOLYGON (((-122.44632 37.86105, -122.4385...
21 23 0400000US08 08 Colorado 103641.888 POLYGON ((-102.04224 36.99308, -102.05450 36.9...
22 24 0400000US09 09 Connecticut 4842.355 MULTIPOLYGON (((-71.85957 41.32240, -71.86823 ...
23 25 0400000US10 10 Delaware 1948.543 MULTIPOLYGON (((-75.55945 39.62981, -75.55910 ...
24 26 0400000US11 11 District of Columbia 61.048 POLYGON ((-77.03860 38.79151, -77.03890 38.800...
25 27 0400000US12 12 Florida 53624.759 MULTIPOLYGON (((-85.15641 29.67963, -85.13740 ...
26 28 0400000US13 13 Georgia 57513.485 POLYGON ((-81.44412 30.70971, -81.44872 30.709...
27 30 0400000US16 16 Idaho 82643.117 POLYGON ((-111.04669 42.00157, -111.41587 42.0...
28 31 0400000US17 17 Illinois 55518.930 POLYGON ((-87.53233 39.99778, -87.53254 39.987...
29 32 0400000US18 18 Indiana 35826.109 POLYGON ((-88.02803 37.79922, -88.02938 37.803...
30 33 0400000US19 19 Iowa 55857.130 POLYGON ((-95.76565 40.58521, -95.75889 40.588...
31 34 0400000US20 20 Kansas 81758.717 POLYGON ((-94.61808 36.99813, -94.62522 36.998...
32 35 0400000US21 21 Kentucky 39486.338 MULTIPOLYGON (((-83.67541 36.60081, -83.67561 ...
33 36 0400000US22 22 Louisiana 43203.905 MULTIPOLYGON (((-88.86507 29.75271, -88.88975 ...
34 37 0400000US27 27 Minnesota 79626.743 POLYGON ((-91.37161 43.50095, -91.37695 43.500...
35 38 0400000US28 28 Mississippi 46923.274 MULTIPOLYGON (((-88.71072 30.25080, -88.65680 ...
36 39 0400000US29 29 Missouri 68741.522 POLYGON ((-89.53910 36.49820, -89.53452 36.491...
37 40 0400000US31 31 Nebraska 76824.171 POLYGON ((-95.76565 40.58521, -95.76853 40.583...
38 41 0400000US33 33 New Hampshire 8952.651 MULTIPOLYGON (((-72.45852 42.72685, -72.45849 ...
39 42 0400000US35 35 New Mexico 121298.148 POLYGON ((-109.05004 31.33250, -109.05017 31.4...
40 43 0400000US38 38 North Dakota 69000.798 POLYGON ((-96.56328 45.93524, -96.57690 45.935...
41 44 0400000US40 40 Oklahoma 68594.921 POLYGON ((-94.61792 36.49941, -94.61531 36.484...
42 45 0400000US41 41 Oregon 95988.013 POLYGON ((-117.22007 44.30138, -117.22245 44.2...
43 46 0400000US45 45 South Carolina 30060.696 POLYGON ((-78.54109 33.85111, -78.55394 33.847...
44 47 0400000US46 46 South Dakota 75811.000 POLYGON ((-96.44341 42.48949, -96.45971 42.486...
45 48 0400000US50 50 Vermont 9216.657 POLYGON ((-72.04008 44.15575, -72.04271 44.152...
46 49 0400000US51 51 Virginia 39490.086 MULTIPOLYGON (((-76.04653 37.95359, -76.04169 ...
47 50 0400000US54 54 West Virginia 24038.210 POLYGON ((-81.96830 37.53780, -81.96540 37.541...
48 51 0400000US56 56 Wyoming 97093.141 POLYGON ((-109.05008 41.00066, -109.17368 41.0...

Centroids and geometry

gdf.centroid
/tmp/ipykernel_3120570/2017122361.py:1: UserWarning: Geometry is in a geographic CRS. Results from 'centroid' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation.

  gdf.centroid
0      POINT (-69.22532 45.36948)
1      POINT (-71.79546 42.25229)
2      POINT (-85.43751 44.35323)
3     POINT (-109.64507 47.03355)
4     POINT (-116.65540 39.35646)
5      POINT (-74.66099 40.18393)
6      POINT (-75.50198 42.93930)
7      POINT (-79.35542 35.53980)
8      POINT (-82.79018 40.29333)
9      POINT (-77.79953 40.87382)
10     POINT (-71.55250 41.67619)
11     POINT (-86.34329 35.84299)
12     POINT (-99.35528 31.49051)
13    POINT (-111.67820 39.32379)
14    POINT (-120.45017 47.38108)
15     POINT (-90.01113 44.63829)
16     POINT (-76.76446 39.03041)
17     POINT (-86.82843 32.78969)
18    POINT (-111.66458 34.29326)
19     POINT (-92.43928 34.89974)
20    POINT (-119.61077 37.24612)
21    POINT (-105.54782 38.99855)
22     POINT (-72.72576 41.62055)
23     POINT (-75.50018 38.99178)
24     POINT (-77.01630 38.90473)
25     POINT (-82.50162 28.64096)
26     POINT (-83.44606 32.64908)
27    POINT (-114.65933 44.38912)
28     POINT (-89.19828 40.06474)
29     POINT (-86.27529 39.90853)
30     POINT (-93.50004 42.07462)
31     POINT (-98.38022 38.48470)
32     POINT (-85.29049 37.52666)
33     POINT (-91.98048 31.05264)
34     POINT (-94.30870 46.31645)
35     POINT (-89.66510 32.75040)
36     POINT (-92.47742 38.36762)
37     POINT (-99.81080 41.52715)
38     POINT (-71.57760 43.68569)
39    POINT (-106.10838 34.42137)
40    POINT (-100.46931 47.44634)
41     POINT (-97.50844 35.58350)
42    POINT (-120.55529 43.93673)
43     POINT (-80.89550 33.90710)
44    POINT (-100.23049 44.43615)
45     POINT (-72.66272 44.07518)
46     POINT (-78.80562 37.51539)
47     POINT (-80.61385 38.64251)
48    POINT (-107.55145 42.99964)
dtype: geometry
gdf.crs
<Geographic 2D CRS: EPSG:4326>
Name: WGS 84
Axis Info [ellipsoidal]:
- Lat[north]: Geodetic latitude (degree)
- Lon[east]: Geodetic longitude (degree)
Area of Use:
- name: World.
- bounds: (-180.0, -90.0, 180.0, 90.0)
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
gdf.to_crs(gdf.estimate_utm_crs()).plot()
<AxesSubplot:>

gdf = gdf.to_crs(gdf.estimate_utm_crs())
gdf.centroid
0      POINT (2359392.452 5305169.775)
1      POINT (2253174.905 4901469.336)
2      POINT (1108124.800 4935453.192)
3      POINT (-765955.563 5348067.290)
4     POINT (-1548246.254 4630302.336)
5      POINT (2065285.016 4612318.097)
6      POINT (1930049.196 4904992.641)
7      POINT (1742974.165 4021487.098)
8      POINT (1368634.294 4510266.503)
9      POINT (1784134.621 4638257.315)
10     POINT (2289313.142 4842131.086)
11     POINT (1102080.849 3988935.477)
12     POINT (-103892.519 3500028.226)
13    POINT (-1115997.209 4520861.031)
14    POINT (-1565541.918 5625943.989)
15      POINT (738620.228 4945674.977)
16     POINT (1910429.070 4447760.368)
17     POINT (1078425.172 3643488.570)
18    POINT (-1230212.721 3954699.221)
19      POINT (550933.347 3861451.888)
20    POINT (-1870826.275 4466915.330)
21     POINT (-590190.342 4392344.096)
22     POINT (2192677.610 4811150.640)
23     POINT (2020359.310 4464239.060)
24     POINT (1889584.643 4429716.358)
25     POINT (1535090.511 3210901.406)
26     POINT (1399285.825 3651528.582)
27    POINT (-1223231.299 5144300.473)
28      POINT (824539.285 4439350.669)
29     POINT (1074696.801 4437810.568)
30      POINT (458939.639 4658274.632)
31       POINT (29865.949 4274404.492)
32     POINT (1181577.693 4182456.487)
33      POINT (598555.176 3435096.661)
34      POINT (399444.786 5126179.145)
35      POINT (812334.714 3626923.994)
36      POINT (547395.056 4245527.933)
37      POINT (-68074.613 4621136.163)
38     POINT (2227578.113 5064712.956)
39     POINT (-710483.211 3886170.809)
40      POINT (-63437.281 5282398.408)
41       POINT (91977.904 3947894.482)
42    POINT (-1716081.976 5249621.330)
43     POINT (1623336.735 3818285.055)
44      POINT (-76107.494 4946849.414)
45     POINT (2128860.270 5085004.659)
46     POINT (1758189.886 4249101.361)
47     POINT (1579306.753 4350654.685)
48     POINT (-688676.799 4864507.482)
dtype: geometry
gdf.crs
<Projected CRS: EPSG:32615>
Name: WGS 84 / UTM zone 15N
Axis Info [cartesian]:
- E[east]: Easting (metre)
- N[north]: Northing (metre)
Area of Use:
- name: Between 96°W and 90°W, northern hemisphere between equator and 84°N, onshore and offshore. Canada - Manitoba; Nunavut; Ontario. Ecuador -Galapagos. Guatemala. Mexico. United States (USA).
- bounds: (-96.0, 0.0, -90.0, 84.0)
Coordinate Operation:
- name: UTM zone 15N
- method: Transverse Mercator
Datum: World Geodetic System 1984 ensemble
- Ellipsoid: WGS 84
- Prime Meridian: Greenwich
gdf['centroid'] = gdf.centroid
gdf.head()
index GEO_ID STATE NAME LSAD CENSUSAREA geometry centroid
0 0 0400000US23 23 Maine 30842.923 MULTIPOLYGON (((2516172.424 5253443.650, 25164... POINT (2359392.452 5305169.775)
1 1 0400000US25 25 Massachusetts 7800.058 MULTIPOLYGON (((2351724.597 4850457.653, 23526... POINT (2253174.905 4901469.336)
2 2 0400000US26 26 Michigan 56538.901 MULTIPOLYGON (((821167.988 5338182.388, 821794... POINT (1108124.800 4935453.192)
3 3 0400000US30 30 Montana 145545.801 POLYGON ((-371533.418 5042503.702, -386687.255... POINT (-765955.563 5348067.290)
4 4 0400000US32 32 Nevada 109781.180 POLYGON ((-1384104.733 4308747.816, -1385163.9... POINT (-1548246.254 4630302.336)
base = gdf.plot()
gdf.centroid.plot(ax=base, color='r')
<AxesSubplot:>

Geometry Attribute

gdf.geometry
0     MULTIPOLYGON (((2516172.424 5253443.650, 25164...
1     MULTIPOLYGON (((2351724.597 4850457.653, 23526...
2     MULTIPOLYGON (((821167.988 5338182.388, 821794...
3     POLYGON ((-371533.418 5042503.702, -386687.255...
4     POLYGON ((-1384104.733 4308747.816, -1385163.9...
5     POLYGON ((2003056.105 4538350.496, 2003083.601...
6     MULTIPOLYGON (((2267648.358 4790204.443, 22689...
7     MULTIPOLYGON (((1438177.023 4038770.483, 14383...
8     MULTIPOLYGON (((1347764.520 4669512.453, 13480...
9     POLYGON ((2009412.336 4556637.291, 2008122.809...
10    MULTIPOLYGON (((2312801.421 4845029.912, 23131...
11    POLYGON ((1514737.024 4109398.060, 1514537.561...
12    MULTIPOLYGON (((92911.351 3092597.856, 92990.0...
13    POLYGON ((-1384104.733 4308747.816, -1381904.7...
14    MULTIPOLYGON (((-1682205.672 5879501.588, -167...
15    MULTIPOLYGON (((693374.944 5210974.031, 693159...
16    MULTIPOLYGON (((1987068.430 4366526.262, 19890...
17    MULTIPOLYGON (((1264668.058 3457276.553, 12625...
18    POLYGON ((-932699.253 4217338.169, -933275.202...
19    POLYGON ((360351.380 4040482.637, 363916.663 4...
20    MULTIPOLYGON (((-2114916.187 4624534.270, -211...
21    POLYGON ((-305537.860 4132535.682, -306632.528...
22    MULTIPOLYGON (((2273709.589 4796000.071, 22727...
23    MULTIPOLYGON (((2000818.277 4534887.709, 20008...
24    POLYGON ((1889905.340 4416707.434, 1889692.665...
25    MULTIPOLYGON (((1260129.872 3309151.351, 12619...
26    POLYGON ((1610127.048 3455093.398, 1609687.082...
27    POLYGON ((-996830.450 4810504.086, -1027552.17...
28    POLYGON ((966857.622 4441852.185, 966910.074 4...
29    POLYGON ((937850.748 4195199.124, 937705.699 4...
30    POLYGON ((265938.864 4496389.554, 266523.302 4...
31    POLYGON ((356020.762 4095889.131, 355386.048 4...
32    MULTIPOLYGON (((1335046.637 4091312.569, 13350...
33    MULTIPOLYGON (((899967.144 3298554.678, 897713...
34    POLYGON ((631645.161 4817734.258, 631214.291 4...
35    MULTIPOLYGON (((912838.208 3354370.602, 918103...
36    POLYGON ((810004.565 4044781.403, 810441.658 4...
37    POLYGON ((265938.864 4496389.554, 265688.248 4...
38    MULTIPOLYGON (((2183943.003 4939995.423, 21838...
39    POLYGON ((-1036062.528 3580182.026, -1033579.7...
40    POLYGON ((223770.899 5093027.974, 222715.445 5...
41    POLYGON ((355100.062 4040560.166, 355306.819 4...
42    POLYGON ((-1431865.838 5199448.159, -1432139.7...
43    POLYGON ((1842940.161 3841258.252, 1841789.432...
44    POLYGON ((217005.965 4709874.192, 215650.454 4...
45    POLYGON ((2176439.257 5107755.756, 2176332.418...
46    MULTIPOLYGON (((1994538.922 4338939.700, 19949...
47    POLYGON ((1476144.128 4212196.934, 1476356.502...
48    POLYGON ((-852129.655 4664855.469, -862571.421...
Name: geometry, dtype: geometry
gdf.set_geometry('centroid').plot()
<AxesSubplot:>

gdf.plot()
<AxesSubplot:>

gdf = gdf.set_geometry('centroid')
gdf.plot()
<AxesSubplot:>

gdf = gdf.set_geometry('geometry')
gdf.plot()
<AxesSubplot:>

Basic Spatial Queries and Operations

Centroid of the lower 49

gdf['dissolve_var'] = 1 # all observations have same value for this variable
us = gdf.dissolve(by='dissolve_var')
us.plot()
<AxesSubplot:>

us.head()
geometry index GEO_ID STATE NAME LSAD CENSUSAREA centroid
dissolve_var
1 MULTIPOLYGON (((-1922552.872 3962305.133, -192... 0 0400000US23 23 Maine 30842.923 POINT (2359392.452 5305169.775)
us_cent = us.centroid
base = gdf.plot()
us_cent.plot(ax=base, color='r')
<AxesSubplot:>

Which states are within 500 miles (804.672 kilometers) of the centroid?

buffer = us_cent.buffer(804.672*1000)
buffer.plot()
<AxesSubplot:>

base = gdf.plot()
buffer.plot(ax=base, color='grey')
us_cent.plot(ax=base, color='r')
<AxesSubplot:>

base = gdf.plot()
buffer.plot(ax=base, color='grey', alpha=0.4)
us_cent.plot(ax=base, color='r')
<AxesSubplot:>

gdf.sindex.query(buffer, predicate='intersects')
array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [19, 36, 12, 39, 41, 31, 21, 37, 28, 30, 15, 34, 48, 44, 40,  3]])
rfirst, states_intersecting = gdf.sindex.query(buffer, predicate='intersects')
gdf.iloc[states_intersecting].plot()
<AxesSubplot:>

gdf[~gdf.index.isin(states_intersecting)].plot()
<AxesSubplot:>

Which states are on the border of the lower 48?

us.envelope
dissolve_var
1    POLYGON ((-2186034.766 2758545.507, 2557956.87...
dtype: geometry
bb = us.envelope
bb.plot()
<AxesSubplot:>

base = bb.plot(color='b')
us.plot(ax=base, color='w') # 2 layers
<AxesSubplot:>

bb_less_49 = bb.difference(us.geometry) # 1 layer
bb_less_49.plot()
<AxesSubplot:>

gdf.sindex.query(bb_less_49, predicate='touches')
array([[ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0],
       [25, 33, 35, 17, 26, 43, 12, 39, 18, 20,  7, 46, 16, 23,  5,  9,
        22, 10, 28, 29,  8, 15,  2, 34,  1,  6, 45, 38,  0, 27, 42, 40,
         3, 14]])
_,border_states = gdf.sindex.query(bb_less_49, predicate='touches')
gdf[gdf.index.isin(border_states)].plot()
<AxesSubplot:>

Creating new attributes

import numpy as np

border_dummy = np.zeros(49, 'int')
border_dummy[border_states]=1
border_dummy
array([1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0,
       1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1,
       0, 1, 1, 0, 0])
gdf['border'] = border_dummy
gdf.plot(column='border', categorical=True, legend=True)
<AxesSubplot:>

gdf.explore(column='border', categorical=True, legend=False)
Make this Notebook Trusted to load map: File -> Trust Notebook
gdf.explore()
Make this Notebook Trusted to load map: File -> Trust Notebook
gdf.explore(column='border', categorical=True)
Make this Notebook Trusted to load map: File -> Trust Notebook
gdf.explore?
Signature: gdf.explore(*args, **kwargs)
Docstring:
Interactive map based on GeoPandas and folium/leaflet.js
Generate an interactive leaflet map based on :class:`~geopandas.GeoDataFrame`
Parameters
----------
column : str, np.array, pd.Series (default None)
    The name of the dataframe column, :class:`numpy.array`,
    or :class:`pandas.Series` to be plotted. If :class:`numpy.array` or
    :class:`pandas.Series` are used then it must have same length as dataframe.
cmap : str, matplotlib.Colormap, branca.colormap or function (default None)
    The name of a colormap recognized by ``matplotlib``, a list-like of colors,
    :class:`matplotlib.colors.Colormap`, a :class:`branca.colormap.ColorMap` or
    function that returns a named color or hex based on the column
    value, e.g.::
        def my_colormap(value):  # scalar value defined in 'column'
            if value > 1:
                return "green"
            return "red"
color : str, array-like (default None)
    Named color or a list-like of colors (named or hex).
m : folium.Map (default None)
    Existing map instance on which to draw the plot.
tiles : str, xyzservices.TileProvider (default 'OpenStreetMap Mapnik')
    Map tileset to use. Can choose from the list supported by folium, query a
    :class:`xyzservices.TileProvider` by a name from ``xyzservices.providers``,
    pass :class:`xyzservices.TileProvider` object or pass custom XYZ URL.
    The current list of built-in providers (when ``xyzservices`` is not available):
    ``["OpenStreetMap", "Stamen Terrain", “Stamen Toner", “Stamen Watercolor"
    "CartoDB positron", “CartoDB dark_matter"]``
    You can pass a custom tileset to Folium by passing a Leaflet-style URL
    to the tiles parameter: ``http://{s}.yourtiles.com/{z}/{x}/{y}.png``.
    Be sure to check their terms and conditions and to provide attribution with
    the ``attr`` keyword.
attr : str (default None)
    Map tile attribution; only required if passing custom tile URL.
tooltip : bool, str, int, list (default True)
    Display GeoDataFrame attributes when hovering over the object.
    ``True`` includes all columns. ``False`` removes tooltip. Pass string or list of
    strings to specify a column(s). Integer specifies first n columns to be
    included. Defaults to ``True``.
popup : bool, str, int, list (default False)
    Input GeoDataFrame attributes for object displayed when clicking.
    ``True`` includes all columns. ``False`` removes popup. Pass string or list of
    strings to specify a column(s). Integer specifies first n columns to be
    included. Defaults to ``False``.
highlight : bool (default True)
    Enable highlight functionality when hovering over a geometry.
categorical : bool (default False)
    If ``False``, ``cmap`` will reflect numerical values of the
    column being plotted. For non-numerical columns, this
    will be set to True.
legend : bool (default True)
    Plot a legend in choropleth plots.
    Ignored if no ``column`` is given.
scheme : str (default None)
    Name of a choropleth classification scheme (requires ``mapclassify`` >= 2.4.0).
    A :func:`mapclassify.classify` will be used
    under the hood. Supported are all schemes provided by ``mapclassify`` (e.g.
    ``'BoxPlot'``, ``'EqualInterval'``, ``'FisherJenks'``, ``'FisherJenksSampled'``,
    ``'HeadTailBreaks'``, ``'JenksCaspall'``, ``'JenksCaspallForced'``,
    ``'JenksCaspallSampled'``, ``'MaxP'``, ``'MaximumBreaks'``,
    ``'NaturalBreaks'``, ``'Quantiles'``, ``'Percentiles'``, ``'StdMean'``,
    ``'UserDefined'``). Arguments can be passed in ``classification_kwds``.
k : int (default 5)
    Number of classes
vmin : None or float (default None)
    Minimum value of ``cmap``. If ``None``, the minimum data value
    in the column to be plotted is used.
vmax : None or float (default None)
    Maximum value of ``cmap``. If ``None``, the maximum data value
    in the column to be plotted is used.
width : pixel int or percentage string (default: '100%')
    Width of the folium :class:`~folium.folium.Map`. If the argument
    m is given explicitly, width is ignored.
height : pixel int or percentage string (default: '100%')
    Height of the folium :class:`~folium.folium.Map`. If the argument
    m is given explicitly, height is ignored.
categories : list-like
    Ordered list-like object of categories to be used for categorical plot.
classification_kwds : dict (default None)
    Keyword arguments to pass to mapclassify
control_scale : bool, (default True)
    Whether to add a control scale on the map.
marker_type : str, folium.Circle, folium.CircleMarker, folium.Marker (default None)
    Allowed string options are ('marker', 'circle', 'circle_marker'). Defaults to
    folium.CircleMarker.
marker_kwds: dict (default {})
    Additional keywords to be passed to the selected ``marker_type``, e.g.:
    radius : float (default 2 for ``circle_marker`` and 50 for ``circle``))
        Radius of the circle, in meters (for ``circle``) or pixels
        (for ``circle_marker``).
    fill : bool (default True)
        Whether to fill the ``circle`` or ``circle_marker`` with color.
    icon : folium.map.Icon
        the :class:`folium.map.Icon` object to use to render the marker.
    draggable : bool (default False)
        Set to True to be able to drag the marker around the map.
style_kwds : dict (default {})
    Additional style to be passed to folium ``style_function``:
    stroke : bool (default True)
        Whether to draw stroke along the path. Set it to ``False`` to
        disable borders on polygons or circles.
    color : str
        Stroke color
    weight : int
        Stroke width in pixels
    opacity : float (default 1.0)
        Stroke opacity
    fill : boolean (default True)
        Whether to fill the path with color. Set it to ``False`` to
        disable filling on polygons or circles.
    fillColor : str
        Fill color. Defaults to the value of the color option
    fillOpacity : float (default 0.5)
        Fill opacity.
    style_function : callable
        Function mapping a GeoJson Feature to a style ``dict``.
        * Style properties :func:`folium.vector_layers.path_options`
        * GeoJson features :class:`GeoDataFrame.__geo_interface__`
        e.g.::
            lambda x: {"color":"red" if x["properties"]["gdp_md_est"]<10**6
                                         else "blue"}
    Plus all supported by :func:`folium.vector_layers.path_options`. See the
    documentation of :class:`folium.features.GeoJson` for details.
highlight_kwds : dict (default {})
    Style to be passed to folium highlight_function. Uses the same keywords
    as ``style_kwds``. When empty, defaults to ``{"fillOpacity": 0.75}``.
tooltip_kwds : dict (default {})
    Additional keywords to be passed to :class:`folium.features.GeoJsonTooltip`,
    e.g. ``aliases``, ``labels``, or ``sticky``.
popup_kwds : dict (default {})
    Additional keywords to be passed to :class:`folium.features.GeoJsonPopup`,
    e.g. ``aliases`` or ``labels``.
legend_kwds : dict (default {})
    Additional keywords to be passed to the legend.
    Currently supported customisation:
    caption : string
        Custom caption of the legend. Defaults to the column name.
    Additional accepted keywords when ``scheme`` is specified:
    colorbar : bool (default True)
        An option to control the style of the legend. If True, continuous
        colorbar will be used. If False, categorical legend will be used for bins.
    scale : bool (default True)
        Scale bins along the colorbar axis according to the bin edges (True)
        or use the equal length for each bin (False)
    fmt : string (default "{:.2f}")
        A formatting specification for the bin edges of the classes in the
        legend. For example, to have no decimals: ``{"fmt": "{:.0f}"}``. Applies
        if ``colorbar=False``.
    labels : list-like
        A list of legend labels to override the auto-generated labels.
        Needs to have the same number of elements as the number of
        classes (`k`). Applies if ``colorbar=False``.
    interval : boolean (default False)
        An option to control brackets from mapclassify legend.
        If True, open/closed interval brackets are shown in the legend.
        Applies if ``colorbar=False``.
    max_labels : int, default 10
        Maximum number of colorbar tick labels (requires branca>=0.5.0)
map_kwds : dict (default {})
    Additional keywords to be passed to folium :class:`~folium.folium.Map`,
    e.g. ``dragging``, or ``scrollWheelZoom``.
**kwargs : dict
    Additional options to be passed on to the folium object.
Returns
-------
m : folium.folium.Map
    folium :class:`~folium.folium.Map` instance
Examples
--------
>>> import geodatasets
>>> df = geopandas.read_file(
...     geodatasets.get_path("geoda.chicago_health")
... )
>>> df.head(2)  # doctest: +SKIP
   ComAreaID  ...                                           geometry
0         35  ...  POLYGON ((-87.60914 41.84469, -87.60915 41.844...
1         36  ...  POLYGON ((-87.59215 41.81693, -87.59231 41.816...
[2 rows x 87 columns]
>>> df.explore("Pop2012", cmap="Blues")  # doctest: +SKIP
File:      /opt/tljh/user/lib/python3.9/site-packages/geopandas/geodataframe.py
Type:      method